//
//  XDCitySelectViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDCitySelectViewController.h"

@interface XDCitySelectViewController ()

/** 所有省份 */
@property(nonatomic,strong) NSMutableArray *allProvinceArr;
/** 所有城市 */
@property(nonatomic,strong) NSMutableArray *allCityArr;
/** 所有区域 */
@property(nonatomic,strong) NSMutableArray *allAreaArr;

/** 年级数据 */
@property(nonatomic,strong) NSMutableArray *gradeArr;

/** 当前数据源 */
@property(nonatomic,strong) NSMutableArray *currentArr;

@end

@implementation XDCitySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _currentArr = [NSMutableArray array];
    [self getAreaData];
    
    if (self.selectType == SelectTypeProvince) {
        self.navigationItem.title = @"选择省份";
        _currentArr = _allProvinceArr;
        
    }else if (self.selectType == SelectTypeCity){
        
        self.navigationItem.title = @"选择城市";
        [self getCityWithPorvinceid:self.provinceid];
        
    }else if(self.selectType == SelectTypeArea){
        
        [self getAreaWithCityid:self.cityid];
        self.navigationItem.title = @"选择区域";
        
    }else if(self.selectType == SelectTypeGrade){
        
        _currentArr = self.gradeArr;
        self.navigationItem.title = @"选择年级";
    }
    
  
}


/**
获取省份城市地域全部信息
*/
-(void)getAreaData{
    
    //获取所有数据
    NSString* filePath1 =[[NSBundle mainBundle] pathForResource:@"province" ofType:@"json"];
    NSData *provinceData = [NSData dataWithContentsOfFile:filePath1 options:NSDataReadingMappedIfSafe error:nil];
    NSMutableArray *province = [NSJSONSerialization JSONObjectWithData:provinceData options:NSJSONReadingMutableContainers error:nil];
    _allProvinceArr = province;
    
    NSString* filePath2 =[[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"];
    NSData *cityData = [NSData dataWithContentsOfFile:filePath2 options:NSDataReadingMappedIfSafe error:nil];
    NSMutableArray *city = [NSJSONSerialization JSONObjectWithData:cityData options:NSJSONReadingMutableContainers error:nil];
    _allCityArr = city;
    
    NSString* filePath3 = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"json"];
    NSData *areaData = [NSData dataWithContentsOfFile:filePath3 options:NSDataReadingMappedIfSafe error:nil];
    NSMutableArray *area = [NSJSONSerialization JSONObjectWithData:areaData options:NSJSONReadingMutableContainers error:nil];
    _allAreaArr = area;
    
}


/**
 根据省份id获取城市数据

 @param provinceid 省份id
 */
-(void)getCityWithPorvinceid:(NSNumber *)provinceid{

    
    for (NSDictionary *dict in _allCityArr) {

        if ([[dict objectForKey:@"provinceid"] isEqualToNumber:provinceid]) {
            
            [self.currentArr addObject:dict];
            
        }
    }
    
}


/**
 根据城市id获取区域数据

 @param cityid 城市id
 */
-(void)getAreaWithCityid:(NSNumber *)cityid{

    for (NSDictionary *dict in _allAreaArr) {
        
        if ([[dict objectForKey:@"cityid"] isEqualToNumber:cityid]) {
            
            [self.currentArr addObject:dict];
            
        }
    }
}


-(NSMutableArray *)gradeArr{

    if (!_gradeArr) {
        _gradeArr = [NSMutableArray arrayWithObjects:
                     @{@"gradeid":@1,@"gradename":@"一年级"},
                     @{@"gradeid":@2,@"gradename":@"二年级"},
                     @{@"gradeid":@3,@"gradename":@"三年级"},
                     @{@"gradeid":@4,@"gradename":@"四年级"},
                     @{@"gradeid":@5,@"gradename":@"五年级"},
                     @{@"gradeid":@6,@"gradename":@"六年级"},
                     nil];
    }
    return _gradeArr;
}

#pragma mark - tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _currentArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellid = @"cityCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    
    NSString *title;
    if (self.selectType == SelectTypeProvince) {    //选择省份
        
        title = _currentArr[indexPath.row][@"provincename"];
        
    }else if (self.selectType == SelectTypeCity){   //选择城市
        
         title = _currentArr[indexPath.row][@"cityname"];
        
    }else if(self.selectType == SelectTypeArea){    //选择区域
        
         title = _currentArr[indexPath.row][@"areaname"];
        
    }else if(self.selectType == SelectTypeGrade){   //选择年级
        
        title = _currentArr[indexPath.row][@"gradename"];
    }

    cell.textLabel.text = title;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSNumber *selectedId;
    NSString *selectedName;
    
    if (self.selectType == SelectTypeProvince) {    //选择省份
        
        selectedId = _currentArr[indexPath.row][@"provinceid"];
        selectedName = _currentArr[indexPath.row][@"provincename"];
        
    }else if (self.selectType == SelectTypeCity){   //选择城市
        
        selectedId = _currentArr[indexPath.row][@"cityid"];
        selectedName = _currentArr[indexPath.row][@"cityname"];
        
    }else if(self.selectType == SelectTypeArea){    //选择区域
        
        selectedId = _currentArr[indexPath.row][@"areaid"];
        selectedName = _currentArr[indexPath.row][@"areaname"];
        
    }else if(self.selectType == SelectTypeGrade){   //选择年级
        
        selectedId = _currentArr[indexPath.row][@"gradeid"];
        selectedName = _currentArr[indexPath.row][@"gradename"];
    }
    
    
    if (self.selectedBlock) {
        self.selectedBlock(selectedId,selectedName);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
